package com.ruoyi.pcj.interfaces.app;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.pcj.constant.TipConstant;
import com.ruoyi.pcj.domain.PcjTbArticle;
import com.ruoyi.pcj.domain.PcjTbPlate;
import com.ruoyi.pcj.domain.PcjTbSubject;
import com.ruoyi.pcj.service.IPcjTbArticleService;
import com.ruoyi.pcj.service.IPcjTbPlateService;
import com.ruoyi.pcj.service.IPcjTbSubjectService;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 贴吧相关信息接口
 */
@RestController
@RequestMapping("/wx/tieBa")
public class TieBaController extends BaseController {

    @Autowired
    private IPcjTbArticleService pcjTbArticleService;

    @Autowired
    private IPcjTbPlateService pcjTbPlateService;

    @Autowired
    private IPcjTbSubjectService pcjTbSubjectService;

    @GetMapping("/test")
    public AjaxResult test(@RequestParam Map<String, Object> params) {
        System.out.println(params);
        return AjaxResult.success("123");
    }



    /**
     * @return com.ruoyi.common.core.page.TableDataInfo
     * @Author Jdike
     * @Description 获取文章列表
     * @Date 23:31 2024/3/11
     * @Param pageNum、pageSize
     **/
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody Map<String, Object> params) {
        IPage<PcjTbArticle> page = pcjTbArticleService.selectPcjTbArticleList(params);
        return getDataTableMp(page);
    }


    @PostMapping("/listTbPlate")
    public TableDataInfo list(PcjTbPlate pcjTbPlate)
    {
        // startPage();
        List<PcjTbPlate> list = pcjTbPlateService.selectPcjTbPlateList(pcjTbPlate);
        return getDataTable(list);
    }

    /**
     * 获取贴吧板块管理详细信息
     */
    @PostMapping(value = "/{listTbPlatePartition}")
    public AjaxResult getInfo(@RequestBody Map<String, Object> params)
    {
        String id = MapUtils.getString(params, "id");
        return success(pcjTbPlateService.selectPcjTbPlateById(id));
    }

    /**
    * @Author Jdike
    * @Description 新增文章
    * @Date 19:24 2024/3/17
    * @Param [pcjTbArticle]
    * @return com.ruoyi.common.core.domain.AjaxResult
    **/
    @PostMapping("/addArticle")
    public AjaxResult add(@RequestBody PcjTbArticle pcjTbArticle)
    {
        pcjTbArticle.setStatus("1");
        return AjaxResult.ok(pcjTbArticleService.insertPcjTbArticle(pcjTbArticle));
    }

    /**
    * @Author Jdike
    * @Description 更新文章
    * @Date 19:24 2024/3/17
    * @Param [pcjTbArticle]
    * @return com.ruoyi.common.core.domain.AjaxResult
    **/
    @PostMapping("/updateArticle")
    public AjaxResult edit(@RequestBody PcjTbArticle pcjTbArticle)
    {
        pcjTbArticle.setStatus("1");
        return AjaxResult.ok(pcjTbArticleService.updatePcjTbArticle(pcjTbArticle));
    }


    /**
    * @Author Jdike
    * @Description 删除文章
    * @Date 19:24 2024/3/17
    * @Param [params]
    * @return com.ruoyi.common.core.domain.AjaxResult
    **/
    @PostMapping("/deleteArticle")
    public AjaxResult remove(@RequestBody Map<String, String> params)
    {
        String id = MapUtils.getString(params, "id");
        return AjaxResult.ok(pcjTbArticleService.deletePcjTbArticleById(id));
    }


    /**
     * @return com.ruoyi.common.core.domain.AjaxResult
     * @Author Jdike
     * @Description 根据id获取文章详细信息
     * @Date 23:32 2024/3/11
     * @Param [id]
     **/
    @PostMapping(value = "/resource")
    public AjaxResult getArticleById(@RequestBody Map<String, String> params) {
        return AjaxResult.ok(pcjTbArticleService.selectPcjTbArticleById(params.get("id")));
    }

    @PostMapping(value = "/resourceComment")
    public AjaxResult getArticleCommentById(@RequestBody Map<String, String> params) {
        return AjaxResult.ok(pcjTbArticleService.selectPcjTbArticleCommentById(params.get("id")));
    }

    /**
     * 文章点赞
     *
     * @return
     */
    @PostMapping(value = "/like")
    public AjaxResult articleLike(@RequestBody Map<String, Object> params) {
        pcjTbArticleService.updateArticleLike(params);
        return AjaxResult.ok(TipConstant.SUCCESS);
    }

    /**
     * 文章收藏
     *
     * @return
     */
    @PostMapping(value = "/collect")
    public AjaxResult articleCollect(@RequestBody Map<String, Object> params) {
        pcjTbArticleService.updateArticleCollect(params);
        return AjaxResult.ok(TipConstant.SUCCESS);
    }

    /**
    * @Author Jdike
    * @Description 保存一级评论
    * @Date 22:45 2024/3/13
    * @Param [params]
    * @return com.ruoyi.common.core.domain.AjaxResult
    **/
    @PostMapping(value = "/comment")
    public AjaxResult articleComment(@RequestBody Map<String, Object> params) {
        pcjTbArticleService.saveArticleComment(params);
        return AjaxResult.ok(TipConstant.SUCCESS);
    }

    /**
    * @Author Jdike
    * @Description 一级评论删除
    * @Date 23:44 2024/3/14
    * @Param [params]
    * @return com.ruoyi.common.core.domain.AjaxResult
    **/
    @PostMapping(value = "/commentDelete")
    public AjaxResult articleCommentDelete(@RequestBody Map<String, Object> params) {
        pcjTbArticleService.deleteArticleComment(params);
        return AjaxResult.ok(TipConstant.SUCCESS);
    }

    /**
     * @Author Jdike
     * @Description 保存二级评论
     * @Date 22:45 2024/3/13
     * @Param [params]
     * @return com.ruoyi.common.core.domain.AjaxResult
     **/
    @PostMapping(value = "/commentReply")
    public AjaxResult articleCommentReply(@RequestBody Map<String, Object> params) {
        pcjTbArticleService.saveArticleCommentReply(params);
        return AjaxResult.ok(TipConstant.SUCCESS);
    }


    /**
    * @Author Jdike
    * @Description 二级评论删除
    * @Date 23:44 2024/3/14
    * @Param [params]
    * @return com.ruoyi.common.core.domain.AjaxResult
    **/
    @PostMapping(value = "/commentReplyDelete")
    public AjaxResult articleCommentReplyDelete(@RequestBody Map<String, Object> params) {
        pcjTbArticleService.deleteArticleCommentReply(params);
        return AjaxResult.ok(TipConstant.SUCCESS);
    }

    @PostMapping("/subjectList")
    public AjaxResult subjectList(@RequestBody Map<String, Object> params)
    {
        String name = MapUtils.getString(params, "name");
        return AjaxResult.ok(pcjTbSubjectService.selectPcjTbSubjectList(name));
    }

}
